﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;

namespace MazeWars
{
    public class AI : Player
    {
        Random random;
        float dt;
        int direction, targetTile, targetSpell;
        List<Spell> spells;

        const float UPDATE_TIME = 0.5f;

        public AI()
        {
            random = new Random();
            dt = 0;
            direction = 1;
            targetSpell = -1;
            targetTile = -1;
        }

        public override Keys update(GameTime gt)
        {
            dt += gt.ElapsedGameTime.Milliseconds / 1000.0f;
            if (dt >= UPDATE_TIME)
            {
                dt = 0;
                if (targetSpell == -1)
                {
                    selectnextSpell();
                }

                if (branchNum == 0 && targetTile < loc[branchNum])
                {
                    direction = -1;
                    button = TrunkList[branchNum].TrunkTiles[loc[branchNum] + direction].Button;
                }
                else if (branchNum == 0 && targetTile > loc[branchNum])
                {
                    direction = 1;
                    button = TrunkList[branchNum].TrunkTiles[loc[branchNum] + direction].Button;
                }
                else if(branchNum == 0 && targetTile == loc[branchNum])
                {
                    button = TrunkList[TrunkList[branchNum].TrunkTiles[loc[branchNum]].AttachedBranch].TrunkTiles[0].Button;
                    direction = 1;
                }
                else if (TrunkList[branchNum].TrunkTiles[loc[branchNum]].Type % 2 == 1 && TrunkList[branchNum].SpellID == targetSpell)
                {
                    direction *= -1;
                    button = TrunkList[branchNum].TrunkTiles[loc[branchNum] + direction].Button;
                }
                else if (branchNum != 0 && loc[branchNum] == 0 && direction == -1)
                {
                    button = prevTrunk(branchNum).TrunkTiles[loc[trunkNumber(prevTrunk(branchNum))]].Button;
                }
                else
                {
                    button = TrunkList[branchNum].TrunkTiles[loc[branchNum] + direction].Button;
                }

                switch (button)
                {
                    case 0: return Keys.Left;
                    case 1: return Keys.Right;
                    case 2: return Keys.Up;
                    case 3: return Keys.Down;
                    case 4: return Keys.A;
                    case 5: return Keys.S;
                    case 6: return Keys.D;
                    case 7: return Keys.W;
                }
            }
            return Keys.None;
        }

        private void selectnextSpell()
        {
            int distance = 0;
            int counter = 0;
            int tempBranchNum = 0;
            float maxWeight = 0;
            float tempWeight = 0;

            foreach (Tile t in TrunkList[0].TrunkTiles)
            {
                distance = 0;
                tempBranchNum = t.AttachedBranch;
                if (tempBranchNum == 0 || TrunkList[tempBranchNum].OnCooldown)
                {
                    counter++;
                    continue;
                }
                distance += Math.Abs(counter - loc[0]) + TrunkList[tempBranchNum].TrunkTiles.Count;
                tempWeight = spells[TrunkList[tempBranchNum].SpellID].Damage / (float)distance;
                if (tempWeight > maxWeight)
                {
                    maxWeight = tempWeight;
                    targetSpell = TrunkList[tempBranchNum].SpellID;
                    targetTile = counter;
                }
                counter++;
            }
        }

        public override int castSpell()
        {
            if (castingSpell)
            {
                targetSpell = -1;
                castingSpell = false;
                return spellID;
            }
            return -1;
        }

        public override void SetSpells(List<Spell> s)
        {
            spells = s;
        }

        public override void reset()
        {
            dt = 0;
            direction = 1;
            base.reset();
        }
    }//End of class AI
}
